package com.question;

import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.locks.Condition;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;

/*一个线程打印 1~52,另一个线程打印字母A-Z。打印顺序为12A34B56C……5152Z。*/
public class Test2 {
    public  static  void  main(String []args)
    {

        final DataAndProcess2 dataAndProcess2=new DataAndProcess2();

        ExecutorService service= Executors.newCachedThreadPool();

        service.execute(
                new Runnable() {
            public void run() {
                for(int i=0;i<26;i++)
                {
                    dataAndProcess2.printNum(i);
                }
            }
        });

        service.execute(
                new Runnable() {
                    public void run() {
                        for(int i=0;i<26;i++)
                        {
                            dataAndProcess2.printABC(i);

                        }
                    }
                }
        );
        service.shutdown();
    }
}
class DataAndProcess2
{
    int flag=0;
    Lock lock=new ReentrantLock();
    Condition condition1=lock.newCondition();
    Condition condition2=lock.newCondition();

    public void printNum(int i)
    {
        lock.lock();
        if (flag!=0)
        {
            try {
                condition1.await();
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
        System.out.println(2*i+1 );
        System.out.println(2*i+2);
        flag=1;
        condition2.signal();
        lock.unlock();
    }
    public void  printABC(int i)
    {
        lock.lock();
        if (flag!=1)
        {
            try {
                condition2.await();
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
        System.out.println((char)(i+65));
        flag=0;
        condition1.signal();
        lock.unlock();

    }


}

